//
//  LunBoView.m
//  礼物说
//
//  Created by dllo on 16/4/21.
//  Copyright © 2016年 sunzhichao. All rights reserved.
//

#import "LunBoView.h"

#define width self.frame.size.width
#define height self.frame.size.height

@interface LunBoView ()<UIScrollViewDelegate>

@property (nonatomic, retain) UIScrollView * scrollView;
@property (nonatomic, retain) UIPageControl * page;
@property (nonatomic, retain) NSArray * wangzhiArr;
@property (nonatomic, retain) NSTimer * timer;
@property (nonatomic, assign) NSInteger num;

@end

@implementation LunBoView

- (instancetype)initWithFrame:(CGRect)frame UrlArr:(NSArray *)urlArr placeholderImage:(NSString *)string Time:(CGFloat)time
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createScrollViewUrlArr:(NSArray *)urlArr placeholderImage:(NSString *)string Time:(CGFloat)time];
    }
    return self;
}
- (void)dealloc{
    [_wangzhiArr release];
    [_page release];
    [_scrollView release];
    [super dealloc];
}
- (void)createScrollViewUrlArr:(NSArray *)urlArr placeholderImage:(NSString *)string Time:(CGFloat)time{
    /**给属性数组赋值，为的是在下面用*/
    self.wangzhiArr = [NSArray arrayWithArray:urlArr];
    
#pragma mark - 创建scrollView
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    [self addSubview:_scrollView];
    [_scrollView release];
    _scrollView.contentSize = CGSizeMake( width * (urlArr.count + 2), 0);
    _scrollView.bounces = NO;
    _scrollView.pagingEnabled = YES;
    /**取消滚动条 */
    _scrollView.showsHorizontalScrollIndicator = YES;
    //    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.delegate = self;
    _scrollView.contentOffset = CGPointMake(width, 0);
    self.num = 0;
    
    for (NSInteger i = 0; i < urlArr.count + 2; i++) {
        
        UIImageView * imgView = [[UIImageView alloc]initWithFrame:CGRectMake(width * i, 0, width, height)];
//        imgView.backgroundColor = [UIColor redColor];
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        imgView.userInteractionEnabled = YES;
        if (i == 0) {
            
            [imgView sd_setImageWithURL:[NSURL URLWithString:urlArr[urlArr.count - 1]] placeholderImage:[UIImage imageNamed:string]];
            
            [imgView addGestureRecognizer:tap];
            
            
        }else if (i >= 1 && i < (urlArr.count + 1)){
            
            [imgView sd_setImageWithURL:[NSURL URLWithString:urlArr[i - 1]] placeholderImage:[UIImage imageNamed: string]];
            [imgView addGestureRecognizer:tap];
            
            
        }else{
            
            [imgView sd_setImageWithURL:[NSURL URLWithString:urlArr[0]] placeholderImage:[UIImage imageNamed:string]];
            [imgView addGestureRecognizer:tap];
            
        }
        [_scrollView addSubview:imgView];
        [imgView release];
        [tap release];
    }
    
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(width / 2 - width / 375 * 100 , height - height / 300  * 40, width / 375 * 200, height / 300  * 40)];
    [self addSubview:_page];
    [_page release];
    _page.numberOfPages = urlArr.count;
    _page.currentPage = 0;
    /**为选中的颜色 */
    _page.pageIndicatorTintColor = [UIColor grayColor];
    /**选中颜色 */
    _page.currentPageIndicatorTintColor = [UIColor whiteColor];
    
    [_page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:time target:self selector:@selector(timeAction) userInfo:nil repeats:YES];
    
    
    [_timer setFireDate:[NSDate distantPast]];
}
- (void)tapAction{
    
    NSInteger i = _scrollView.contentOffset.x / width - 1;
    
    [self.delegate selectImageIndex:i];
}

- (void)timeAction{

    self.num++;
    if (self.num == self.wangzhiArr.count + 1) {
        self.num = 1;
    }
    self.scrollView.contentOffset = CGPointMake(width * self.num, 0);
    self.page.currentPage = self.num - 1;
    
}
- (void)pageAction:(UIPageControl *)page{
    
    [_scrollView setContentOffset: CGPointMake((page.currentPage + 1)* width, 0) animated:YES];
    
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.timer setFireDate:[NSDate distantFuture]];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    if (scrollView.contentOffset.x / width == 0) {
        
        [_scrollView setContentOffset:CGPointMake(width * _wangzhiArr.count, 0) animated:NO];
        
    }
    if (scrollView.contentOffset.x / width == _wangzhiArr.count + 1) {
        
        [_scrollView setContentOffset:CGPointMake(width, 0) animated:NO];
    }
    self.page.currentPage = scrollView.contentOffset.x / width - 1;
    self.num = scrollView.contentOffset.x / width - 1;
    [self.timer setFireDate:[NSDate distantPast]];
    
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
